import imapclient
from email.header import decode_header
from datetime import date
import eventlet
eventlet.monkey_patch()

def get_mail():
    with eventlet.Timeout(10,False):
        #设置超时时间
        client = imapclient.IMAPClient('imap.qq.com', ssl=True)
        client.login('1779626055@qq.com', 'uosvmcthjhrtghii')
        client.select_folder('INBOX', readonly=True)
        # 读取 2021 1月1日后的未读邮件 
        UIDs = client.search([u'SINCE', date(2021, 1, 1), 'UNSEEN'])
        # UIDs.reverse()
        # 记录邮件条数
        count = 0
        subjects = []
        for i in UIDs:
            count += 1
            msg = client.fetch(i,  ['ENVELOPE'])
            envelope = msg[i][b'ENVELOPE']
            subject = envelope.subject
            subject, decode = decode_header(subject.decode())[0]
            subject = subject.decode(decode)
            subjects.append(subject)
            print(subject)
        # print(msgdict)
        print(count)
        # print(UIDs)
        return {"subjects":subjects,"count":count}

    #获取超时
    return {"subjects":[],"count":0}
if __name__ == "__main__":
    get_mail()
